/**
  @authoer: lss
  @date: 2020/12/29
  @note:
**/
package common

import (
	"encoding/json"
	"github.com/astaxie/beego/logs"
	"github.com/gin-gonic/gin"
	"wonlicsys/api/v1"
)

func FormatResponse(errCode int, errMsg string, data interface{}) map[string]interface{} {
	jsonData := make(map[string]interface{}, 3)
	jsonData["Code"] = errCode
	jsonData["Msg"] = errMsg
	jsonData["Data"] = data
	return jsonData
}

func GetParamStruct(c *gin.Context, obj interface{}) error {
	//var t = make(map[string]interface{})
	////判断请求方式
	//method := c.Request.Method
	////校验签名
	//Sign := c.Request.Header.Get("Sign")
	//if len(Sign) == 0 {
	//	return errors.New("Sign 不能为空")
	//}
	////判断类型
	//switch method {
	//case "GET":
	//	c.ShouldBind(obj)
	//	//校验签名
	//	if err := tool.CheckoutSign(obj,Sign); err != nil{
	//		return err
	//	}
	//case "POST":
	//	if err := c.ShouldBind(&t);err != nil{
	//		return err
	//	}
	//	//校验签名
	//	if err := tool.CheckoutSign(t,Sign); err != nil{
	//		return err
	//	}
	//	reqJson, err := json.Marshal(t)
	//	if err != nil{
	//		return err
	//	}
	//	err = json.Unmarshal(reqJson,&obj)
	//	if err != nil{
	//		return err
	//	}
	//}
	c.ShouldBind(obj)

	if v1.Conf.RunMode == "dev" {
		data, err := json.Marshal(&obj)
		if err != nil {
			return err
		}
		logs.Debug("reqest data ---->:", string(data))
	}
	logs.Debug(obj)
	if err := VerifyForm(obj, "chn"); err != nil {
		logs.Error(err)
		return err
	}

	return nil
}

func Checkout(obj interface{}) error {
	if err := VerifyForm(obj, "chn"); err != nil {
		logs.Error(err)
		return err
	}
	return nil
}

func JsonResult(c *gin.Context, errCode int, errMsg string, data interface{}) {
	if v1.Conf.RunMode == "dev" {
		logs.Debug("reqest data ---->:", errMsg)
	}
	c.JSON(errCode, FormatResponse(errCode, errMsg, data))
}
